# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="yangqibin"
__date__ ="$Nov 12, 2011 7:35:27 PM$"

import web
import res_code
import models
from models import login

t_globals = { 'datestr':web.datestr }

render = web.template.render('templates', globals=t_globals)

class Login:
    form = web.form.Form(
      web.form.Textbox('username', web.form.notnull, description='Username'),
      web.form.Password('password', web.form.notnull, description='Password'),
      web.form.Button('login'),
#      web.form.Button('register'),
    )

    def GET(self):
        f = self.form()
        return render.login(f, res_code.success)

    def POST(self):
        form = self.form()
        if not form.validates():
            return render.login(form, res_code.get_extra_info(res_code.err_validate_failed))

        res = models.login.login(form.d.username, form.d.password)
        if res == 0:            
            raise web.seeother('/index')
#            posts = models.posts.get_posts()
#            return render.index(posts)
        else:
            return render.login(form, res_code.get_extra_info(res))

#    def render_dispatch(self, priviledge):



if __name__ == "__main__":
    print "Hello World"
